Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix order of operands. #34

Merged
merged 8 commits into from
Aug 7, 2024
Merged

Fix order of operands. #34

merged 8 commits into from
Aug 7, 2024

Conversation

Linda-Njau
Copy link

This PR addresses the issue described in issue #26 by correcting the order of operands in the JSON output. Previously, operands were extracted from the function clause, but this change extracts them from the right-hand side of the assembly clause. Additionally, non-operand inputs are now filtered out.

To properly match operands to their respective types, the structure of operands stored in the Hashtbl operands has been updated. The previous structure was a simple list of names:

WXTYPE: funct6, vm, vs2, rs1, vd

The new structure stores a list of tuples, each containing a name and its type value:

WXTYPE: (vd, regidx), (vs2, regidx), (rs1, regidx), (vm, bits(1))

From the example above note:

  1. funct6, which is not an operand, is now correctly filtered out with this update.
  2. The change of operand order.
  3. The change of operand structure.

Copy link
Owner

@ThinkOpenly ThinkOpenly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you've gotten good results. I'd like to understand the code a bit better.

src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
@Linda-Njau
Copy link
Author

I have also renamed the function for more clarity.

  • pre-filter to filter_non_operands
  • extract to extract_operands
  • extract_operands to extract_and_map_operands

Copy link
Owner

@ThinkOpenly ThinkOpenly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd sent some of these comments before, but it appears that I did not. Getting very close!

src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
Comment on lines 248 to 254
try
let comma_index = String.index operand ',' in
debug_print ("Operand before trimming: " ^ operand);
let trimmed = String.sub operand 0 comma_index in
debug_print ("Final trimmed operand: " ^ trimmed);
let inputl = Hashtbl.find inputs k in
if List.mem trimmed inputl then aux (trimmed :: acc) tl else aux acc tl
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this a more generic loop that looks at each element in the list of parameters of this function? For example, if it encounters a string "func(a,b,c)", it checks to see if "a", "b", and "c" are in "inputs"?

@Linda-Njau
Copy link
Author

@ThinkOpenly, I made the changes to address your comments.

Copy link
Owner

@ThinkOpenly ThinkOpenly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion to change where regex is defined/used.
Also, there was one previous comment still open: in cases where you are finding operands looking like A(B,C), check both B and C to see if they need to be preserved.

src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
src/sail_json_backend/json.ml Outdated Show resolved Hide resolved
let comma_index = String.index operand ',' in
debug_print ("Operand before trimming: " ^ operand);
let trimmed = String.sub operand 0 comma_index in
debug_print ("Final trimmed operand: " ^ trimmed);
let inputl = Hashtbl.find inputs k in
if List.mem trimmed inputl then aux (trimmed :: acc) tl else aux acc tl
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed trimmed above, so I'm not sure how you can use it here? :-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed : )

Comment on lines 235 to 240
let filter_non_operands components =
let rec aux acc = function
| [] -> List.rev acc
| hd :: tl -> if String.trim hd = "spc" then tl else aux (hd :: acc) tl
in
aux [] components
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a stab at an alternative implementation that doesn't use List.rev:

let rec filter_non_operands components = match components with
  | [] -> []
  | hd :: tl when String.trim hd = "spc" -> tl
  | _ -> filter_non_operands (List.tl components)

What do you think? It's slightly smaller, doesn't require an embedded aux function, and doesn't use List.rev, so it could be slightly faster (although we're not terribly concerned with performance at this point).

I think it could be adapted to be used in extract_operands, below, as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's much more readable. I've adapted it for extact_operands

Comment on lines 248 to 252
try
let inputl = Hashtbl.find inputs k in
let filtered_elements = List.filter (fun element -> List.mem element inputl) elements in
filtered_elements @ extract_operands k tl
with Not_found -> extract_operands k tl
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, a little bit "peeling the onion" here. I tend to avoid try blocks if there is a reasonable and straightforward alternative, and there's a bit of redundancy in those last two lines. What do you think about this alternative?

+          let filtered_elements = match Hashtbl.find_opt inputs k with
+              | None -> []
+              | Some inputl -> List.filter (fun element -> List.mem element inputl) elements
+          in filtered_elements @ extract_operands k tl

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, I'll adjust and squash.

Copy link
Owner

@ThinkOpenly ThinkOpenly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last few comments, if you'll indulge me. :-)

src/sail_json_backend/json.ml Show resolved Hide resolved
Comment on lines 246 to 247
let operand = Str.matched_group 1 hd in
let elements = Str.split (Str.regexp ",") operand in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor and subjective, but "operand" isn't the best variable name here. Since it is only used in the next line, anyway, let's combine these two lines:

let elements = Str.split (Str.regexp ",") (Str.matched_group 1 hd) in

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done : )

@ThinkOpenly ThinkOpenly merged commit 454f4c8 into ThinkOpenly:json Aug 7, 2024
3 of 4 checks passed
ThinkOpenly pushed a commit that referenced this pull request Sep 16, 2024
* Change extraction of assembly operands from AST input list to assembly order.
  This avoids including extraneous AST inputs in the list of operands, and ensures operands are extracted in the same order as seen in the assembly syntax.
* Take care to extract operands embedded within functions, like `funct(op1 @ op2 @ 0b00)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants